import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import Login from "../views/Login.vue";
import { Dialog } from "vant";
Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  undefined;
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch((err) => err);
};

const routes = [
  {
    path: "/",
    name: "Login",
    component: Login,
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
  },
  {
    path: "/order",
    name: "Order",
    component: function () {
      return import(/* webpackChunkName: "about" */ "../views/Order.vue");
    },
  },
];

const router = new VueRouter({
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  // 此处进行鉴权操作
  let token;
  if (JSON.parse(sessionStorage.getItem("userEat")) != null) {
    token = JSON.parse(sessionStorage.getItem("userEat"));
  } else {
    token = "";
  }

  // 测试这里的token设置为true
  token = true;

  if (!token) {
    if (to.path === "/") {
      next(); // 系统根路由
    } else if (to.path === "/register") {
      next();
    } else {
      Dialog.confirm({ title: "提示", message: "您还没有登录,请先登录!" });
      next("/");
    }
  } else {
    next();
  }
});
export default router;
